package com.dream.keigezhushou.Activity.wigdt;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 * 线程池的管理类
 */

public class MyThreadPoolManager {

    private MyThreadPoolManager(){}

    private static MyThreadPoolManager instance ;

    public synchronized  static MyThreadPoolManager getInstance() {
        if(instance == null) {
            instance = new MyThreadPoolManager();
        }
        return instance;
    }


    private ThreadPoolExecutor executor;

    public void execute(Runnable r){
        if(executor == null) {

            executor = new ThreadPoolExecutor(
                    3,5,0,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(20),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.AbortPolicy());
            int cpuCount = Runtime.getRuntime().availableProcessors();
            int corePoolSize = cpuCount*2 + 1;

        }
        //把任务丢到线程池里面去
        executor.execute(r);

    }

    public void cancle(Runnable runnable) {
        if(executor != null) {
            executor.getQueue().remove(runnable);
        }
    }
}
